﻿using System;

namespace _4.形状类
{
    public abstract class Shape
    {
        public abstract double Area { get; }
    }

    public class Rectangular : Shape
    {
        public Rectangular() { this.length = 4; this.width = 3; }
        public Rectangular(double length,double width)
        {
            this.length = length;
            this.width = width;
        }
        public double length { get; set; }
        public double width { get; set; }

        public override double Area
        {
            get
            {
                return length * width;
            }
        }
    }

    public class Square : Shape
    {
        public Square() { this.length = 4; }
        public Square(double length)
        {
            this.length = length;
        }

        public double length { get; set; }

        public override double Area
        {
            get { return length * length; }
        }
    }

    public class Triangle : Shape
    {
        public Triangle() { this.edge1 = this.edge2 = this.edge3 = 3; }
        public Triangle(double edge1,double edge2,double edge3)
        {
            this.edge1 = edge1;
            this.edge2 = edge2;
            this.edge3 = edge3;

            if (edge1 + edge2 <= edge3 || edge1 + edge3 <= edge2 || edge2 + edge3 <= edge1)
            {
                isCorrect = false;
                Console.WriteLine("创建的三角形不合法");
            }

        }
        public double edge1 { get; set; }
        public double edge2 { get; set; }
        public double edge3 { get; set; }

        private bool isCorrect = true;

        public override double Area
        {
            get
            {
                if(isCorrect)
                {
                    double p = (edge1 + edge2 + edge3) / 2;
                    return Math.Sqrt(p * (p - edge1) * (p - edge2) * (p - edge3));
                }
                else
                {
                    Console.WriteLine("该三角形不合法，无法求面积");
                    return -1;
                }
            }
        }

    }


    class Factory
    {
        public Shape GetShape(string arg)
        {
            Random rd = new Random();
            if(arg== "rectangular")
            {
                double length = rd.Next(10, 30), width = rd.Next(5, 15);
                return new Rectangular();
            }
            else if(arg== "square")
            {
                double length = rd.Next(10, 30);
                return new Square(length);
            }
            else
            {
                double edge1 = rd.Next(10, 30), edge2 = rd.Next(10, 30), edge3 = rd.Next(10, 30);
                return new Triangle(edge1, edge2, edge3);
            }
        }
    }


    class Program
    {
        enum Shapes { rectangular, square, triangle };
        static void Main(string[] args)
        {
            Factory factory = new Factory();
            for(int i=1;i<=10;++i)
            {
                Random rd = new Random();
                int num = rd.Next(0, 3);
                Shape shape;
                string shapeName="";
                switch(num)
                {
                    case 0: shapeName = "rectangular"; break;
                    case 1: shapeName = "square";  break;
                    case 2: shapeName = "triangle";  break;
                    //default: shapeName = "rectangular";
                }
                shape = factory.GetShape(shapeName);
                Console.WriteLine("第{0}次随机产生的形状是：{1}", i, shapeName);
                Console.WriteLine("{0}的面积为：{1:0.###}\n", shapeName, shape.Area);
            }
        }
    }
}
